// /////////////////////////////////////////////////////////////////////////////
//
// Name:            MainFrame.cpp
// Author:          Michael Bartsch
//
// Desc :           This file contains the CMainFrame Class. This class contains
//                  all data and behaviour of the main frame.
//
// License:         Copyright (C) 2008-2009 Michael Bartsch
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the GNU General Public
//                  License as published by the Free Software Foundation,
//                  either version 3 of the License, or (at your option) any
//                  later version.
//
//                  This program is distributed in the hope that it will be
//                  useful, but WITHOUT ANY WARRANTY; without even the implied
//                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//                  PURPOSE. See the GNU General Public License for more
//                  details.
//
//                  You should have received a copy of the GNU General Public
//                  License along with this program. If not, see
//                  <http://www.gnu.org/licenses/>.
//
// /////////////////////////////////////////////////////////////////////////////

// Include required header file(s).
#include "MainFrame.h"


// CMainFrame Events. - [ BEGIN ]
BEGIN_EVENT_TABLE(CMainFrame, wxFrame)
   EVT_CLOSE(CMainFrame::onClose)
   EVT_TIMER(MF_CameraTimer, CMainFrame::onTimer)

   EVT_MENU(wxID_EXIT,               CMainFrame::onQuit)

   EVT_MENU(MF_LuaTerminal,          CMainFrame::onLuaTerminal)
   EVT_MENU(MF_ColourAnalyser,       CMainFrame::onColourAnalyser)
   EVT_MENU(MF_AIControl,            CMainFrame::onAIControl)
   EVT_MENU(MF_Settings,             CMainFrame::onSettings)

   EVT_MENU(wxID_ABOUT,              CMainFrame::onAbout)
END_EVENT_TABLE()
// CMainFrame Events. - [ END ]


// CMainFrame.
// Constructor.
CMainFrame::CMainFrame()
#ifdef __OS_WINDOWS__
: wxFrame((wxFrame *)NULL, 0, _T("Aia|CP"), wxPoint(0,0), wxSize(385,660), wxDEFAULT_FRAME_STYLE & ~ (wxRESIZE_BORDER | wxRESIZE_BOX | wxMAXIMIZE_BOX))
#else
: wxFrame((wxFrame *)NULL, 0, _T("Aia|CP"), wxPoint(0,0), wxSize(385,650), wxDEFAULT_FRAME_STYLE & ~ (wxRESIZE_BORDER | wxRESIZE_BOX | wxMAXIMIZE_BOX))
#endif
{
    // Tell wxAuiManager to manage this frame.
    frameManager.SetManagedWindow(this);
    frameManager.SetFlags(wxAUI_MGR_DEFAULT & ~ wxAUI_MGR_ALLOW_FLOATING);

    // Init the Menu Bar.
    wxMenuBar* menuBar = new wxMenuBar();

    // Menu->File Menu
    wxMenu *fileMenu = new wxMenu;

    wxMenuItem *fmQuit = new wxMenuItem(fileMenu, wxID_EXIT, _T("&Quit\tCtrl+Q"), _T("Exit Aia|CP"));
    fmQuit->SetBitmap(wxIcon("../media/file-quit.png", wxBITMAP_TYPE_PNG));
    fileMenu->Append(fmQuit);

    // Menu->Tools Menu
    wxMenu *toolsMenu = new wxMenu;

    wxMenuItem *tmLuaTerminal = new wxMenuItem(toolsMenu, MF_LuaTerminal, _T("&Lua Terminal\tAlt+L"), _T("Launch the Lua Prompt"));
    tmLuaTerminal->SetBitmap(wxIcon("../media/tools-terminal.png", wxBITMAP_TYPE_PNG));

    wxMenuItem *tmColourAnalyser = new wxMenuItem(toolsMenu, MF_ColourAnalyser, _T("&Colour Analyser\tAlt+C"), _T("Launch the Colour Analyser"));
    tmColourAnalyser->SetBitmap(wxIcon("../media/tools-colour.png", wxBITMAP_TYPE_PNG));

    wxMenuItem *tmAIControl = new wxMenuItem(toolsMenu, MF_AIControl, _T("&AI Control\tAlt+A"), _T("Launch the AI Control"));
    tmAIControl->SetBitmap(wxIcon("../media/tools-ai.png", wxBITMAP_TYPE_PNG));

    wxMenuItem *tmSettings = new wxMenuItem(toolsMenu, MF_Settings, _T("&Settings\tAlt+S"), _T("Settings for Aia|CP"));
    tmSettings->SetBitmap(wxIcon("../media/tools-settings.png", wxBITMAP_TYPE_PNG));

    toolsMenu->Append(tmLuaTerminal);
    toolsMenu->Append(tmColourAnalyser);
    toolsMenu->Append(tmAIControl);
    toolsMenu->AppendSeparator();
    toolsMenu->Append(tmSettings);

    // Menu->Help Menu
    wxMenu *helpMenu = new wxMenu;

    helpMenu->Append(wxID_ABOUT, _T("&About"), _T("About Aia|CP"));

    // Add submenu's to the Menu Bar.
    menuBar->Append(fileMenu, _T("&File"));
    menuBar->Append(toolsMenu, _T("&Tools"));
    menuBar->Append(helpMenu, _T("&Help"));

    // Associate the Menu Bar with the frame.
    SetMenuBar(menuBar);

    // Init the Status Bar.
    wxStatusBar *statusBar = CreateStatusBar(1, 0, -1);

    int *statusBarArray = new int[1];
    statusBarArray[0] = -3;
    statusBar->SetStatusWidths(1, statusBarArray);
    delete[] statusBarArray;

    // Create the panes.
    // Camera Pane
    cameraPanel = new CCameraPanel(this);

    // Log Pane
    logPane = new wxTextCtrl(this, wxID_ANY, _T(""), wxDefaultPosition, wxSize(382, 288), wxTE_MULTILINE | wxTE_READONLY | wxALWAYS_SHOW_SB | wxVSCROLL);

    // Add the panes.
    frameManager.AddPane(cameraPanel, wxAuiPaneInfo().Name(wxT("paneCamera")).
                         CaptionVisible(false).Top().CloseButton(false).
                         MaximizeButton(false).Resizable(false));

    frameManager.AddPane(logPane, wxAuiPaneInfo().Name(wxT("paneLog")).
                         Caption(wxT("Log")).Bottom().CloseButton(false).
                         MaximizeButton(false).Resizable(false));

    frameManager.Update();

    // Set variables.
	pColourAnalyser = CColourAnalyser::Instance();
	pCommunicator = CCommunicator::Instance();
	pAICore = CAICore::Instance();

    colourFrame = NULL;
    luaFrame = NULL;
    aiFrame = NULL;

	camTimer = new wxTimer(this, MF_CameraTimer);
	camTimer->Start(1000, wxTIMER_CONTINUOUS);
}

// Event Functions. - [ BEGIN ]
void CMainFrame::onClose(wxCloseEvent &event)
{
   // Deinitialise wxAuiManager.
   frameManager.UnInit();

   // Destroy the frame.
   Destroy();
}

void CMainFrame::onTimer(wxTimerEvent& event)
{
	pColourAnalyser->updateImage();
	pColourAnalyser->analyseImage();

	if(colourFrame != NULL)
	  colourFrame->update();

	pAICore->update();

	if(aiFrame != NULL)
	  aiFrame->update();

    Refresh();
}

void CMainFrame::onQuit(wxCommandEvent &event)
{
    Close(true);
}

void CMainFrame::onLuaTerminal(wxCommandEvent& event)
{
    // Open the Colour Analyser.
    if(luaFrame == NULL)
    {
       luaFrame = new CLuaFrame(this);
       luaFrame->Centre();
       luaFrame->Show(true);
       luaFrame->SetFocus();
    }
}

void CMainFrame::onColourAnalyser(wxCommandEvent& event)
{
    // Open the Colour Analyser.
    if(colourFrame == NULL)
    {
       colourFrame = new CColourFrame(this);
       colourFrame->Centre();
       colourFrame->Show(true);
       colourFrame->SetFocus();
    }
}

void CMainFrame::onAIControl(wxCommandEvent& event)
{
   // Open the AI Control.
   if(aiFrame == NULL)
   {
      aiFrame = new CAIFrame(this);
      aiFrame->Centre();
      aiFrame->Show(true);
      aiFrame->SetFocus();
   }
}

void CMainFrame::onSettings(wxCommandEvent& event)
{
   CSettingsFrame *settingsFrame = new CSettingsFrame(this);
   settingsFrame->Centre();
   settingsFrame->MakeModal(true);
   settingsFrame->Show(true);
   settingsFrame->SetFocus();
}

void CMainFrame::onAbout(wxCommandEvent& event)
{
   CAboutFrame *aboutFrame = new CAboutFrame(this);
   aboutFrame->Centre();
   aboutFrame->MakeModal(true);
   aboutFrame->Show(true);
   aboutFrame->SetFocus();
}
// Event Functions. - [ END ]

// Get Functions. - [ BEGIN ]
// Get Functions. - [ END ]

// Set Functions - [ BEGIN ]
void CMainFrame::setLogMessage(const char *message)
{
   logPane->AppendText(message);
   logPane->AppendText(wxT("\n"));
}

void CMainFrame::resetLuaFrame()
{
   luaFrame = NULL;
}

void CMainFrame::resetColourFrame()
{
   colourFrame = NULL;
}

void CMainFrame::resetAIFrame()
{
   aiFrame = NULL;
}
// Set Functions = [ END ]

// End of File
